.\" Copyright (c) 2009-2012 Hypertriton, Inc. <http://hypertriton.com/>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
.\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
.\" (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd August 29, 2009
.Dt AG_TIME 3
.Os
.ds vT Agar API Reference
.ds oS Agar 1.3
.Sh NAME
.Nm AG_Time
.Nd agar time interface
.Sh SYNOPSIS
.Bd -literal
#include <agar/core.h>
.Ed
.Sh DESCRIPTION
These functions provide a low-level interface to monotonically increasing
time sources.
Different time backends may be implemented (see
.Sx INTERNAL API
below).
Agar provides the following backends:
.Pp
.Bl -tag -compact -width "agTimeOps_gettimeofday "
.It agTimeOps_dummy
No-op, timers will be unavailable.
.It agTimeOps_posix
Monotonic clock using
.Fn clock_gettime
interface
.It agTimeOps_gettimeofday
Traditional
.Fn gettimeofday
interface.
.It agTimeOps_win32
Windows winmm API.
.It agTimeOps_renderer
Monotonic clock which stops while graphical rendering is performed.
This is useful for applications performing offline rendering, where the
outcome may be influenced by different threads relying on Agar timers
or
.Fn AG_Delay
calls.
.El
.Sh INTERFACE
.nr nS 1
.Ft "Uint32"
.Fn AG_GetTicks "void"
.Pp
.Ft "void"
.Fn AG_Delay "Uint32 t"
.Pp
.Ft void
.Fn AG_SetTimeOps "const AG_TimeOps *ops"
.Pp
.nr nS 0
The
.Fn AG_GetTicks
function returns the current time in ticks.
One tick usually corresponds to one millisecond.
.Pp
The
.Fn AG_Delay
function blocks the current thread, waiting at least
.Fa t
ticks before returning.
The exact amount of time which
.Fn AG_Delay
waits is backend-dependent.
.Pp
The
.Fn AG_SetTimeOps
function selects a time backend (see below).
.\" MANLINK(AG_TimeOps)
.Sh BACKEND INTERFACE
The argument to
.Fn AG_SetTimeOps
should point to the following structure:
.Bd -literal
typedef struct ag_time_ops {
	const char *name;
	void   (*Init)(void);
	void   (*Destroy)(void);
	Uint32 (*GetTicks)(void);
	void   (*Delay)(Uint32);
} AG_TimeOps;
.Ed
.Pp
.Fn Init
performs any necessary initialization.
.Fn Destroy
cleans up any allocated resources.
.Pp
The
.Fn GetTicks
operation is the backend to
.Fn AG_GetTicks
and
.Fn Delay
is the backend to
.Fn AG_Delay .
.Sh EXAMPLES
The following code fragment selects the rendering-aware time backend,
.Va agTimeOps_renderer ,
if it is available:
.Bd -literal -offset indent
#include <agar/config/have_clock_gettime.h>
#include <agar/config/have_pthreads.h>

#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_PTHREADS)
AG_SetTimeOps(&agTimeOps_renderer);
#endif
.Ed
.Sh SEE ALSO
.Xr AG_Intro 3 ,
.Xr AG_Timer 3
.Sh HISTORY
The
.Nm
interface first appeared in Agar 1.3.4
